import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ThemeProvider with ChangeNotifier {
  static const String _themeColorKey = 'theme_color';
  static const String _themeModeKey = 'theme_mode';
  
  // 预定义的主题颜色
  static const List<Color> themeColors = [
    Color(0xFFFF5722), // 橙红色
    Color(0xFFFFB800), // 金黄色
    Color(0xFF16BAAA), // 青绿色
    Color(0xFF1E9FFF), // 蓝色
    Color(0xFFA233C6), // 紫色
    Color(0xFF23292E), // 深色
    Color(0xFFFAFAFA), // 浅色
  ];

  // 当前主题颜色
  Color _currentColor = const Color(0xFF1E9FFF); // 默认蓝色
  ThemeMode _themeMode = ThemeMode.system; // 默认跟随系统

  Color get currentColor => _currentColor;
  ThemeMode get themeMode => _themeMode;

  // 构造函数中加载保存的主题
  ThemeProvider() {
    _loadSavedTheme();
  }

  // 加载保存的主题
  Future<void> _loadSavedTheme() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final colorValue = prefs.getInt(_themeColorKey);
      final themeModeValue = prefs.getString(_themeModeKey);
      
      if (colorValue != null) {
        _currentColor = Color(colorValue);
      }
      
      if (themeModeValue != null) {
        _themeMode = ThemeMode.values.firstWhere(
          (e) => e.toString() == themeModeValue,
          orElse: () => ThemeMode.system,
        );
      }
      
      notifyListeners();
    } catch (e) {
      debugPrint('Error loading theme: $e');
    }
  }

  // 保存主题设置
  Future<void> _saveTheme() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setInt(_themeColorKey, _currentColor.value);
      await prefs.setString(_themeModeKey, _themeMode.toString());
    } catch (e) {
      debugPrint('Error saving theme: $e');
    }
  }

  // 更新主题颜色
  Future<void> updateThemeColor(Color color) async {
    _currentColor = color;
    notifyListeners();
    await _saveTheme();
  }

  // 更新主题模式
  Future<void> updateThemeMode(ThemeMode mode) async {
    _themeMode = mode;
    notifyListeners();
    await _saveTheme();
  }

  // 获取基于当前颜色的主题数据
  ThemeData getTheme({Brightness? brightness}) {
    final isDark = brightness == Brightness.dark || 
                  (_themeMode == ThemeMode.dark) ||
                  (_themeMode == ThemeMode.system && 
                   WidgetsBinding.instance.window.platformBrightness == Brightness.dark);

    return ThemeData(
      useMaterial3: true,
      colorScheme: ColorScheme.fromSeed(
        seedColor: _currentColor,
        brightness: isDark ? Brightness.dark : Brightness.light,
      ),
    );
  }

  // 加载保存的主题设置
  Future<void> loadThemePreferences() async {
    final prefs = await SharedPreferences.getInstance();
    
    // 加载主题色
    final colorValue = prefs.getInt(_themeColorKey);
    if (colorValue != null) {
      _currentColor = Color(colorValue);
    }

    // 加载主题模式
    final themeModeString = prefs.getString(_themeModeKey);
    if (themeModeString != null) {
      _themeMode = ThemeMode.values.firstWhere(
        (mode) => mode.toString() == themeModeString,
        orElse: () => ThemeMode.system,
      );
    }
  }
} 